<?php
if( !$PARAPARA || !$PARAPARA->is_admin() )
    return;

$pagesize = 50;
quot_topmenu();

echo "<div id='padm_quotes'>\n";
echo "<h2>Quotes</h2>\n";

$do = $_GET['do'];
if(!$do)
    $do = 'list';

if( $do == 'list' ) {
    quot_list( 1, $pagesize );
}
elseif( $do == 'new' ) {
    quot_editform( null );
}
elseif( $do == 'form_action') {
    if( $_POST['cancel'])
        quot_list(1, $pagesize );
    else {
        $param = quot_validate_form();
        if( $param['status'] != 'ok' ) {
            quot_editform( $param );
        }
        else {
            quot_update_db( $param );
            echo "<div>all-right</div>";
        }
    }
}
elseif ( $do == 'edit' ) {
    $id = $_GET['id'];
    if( preg_match('/^\d+$/', $id) ) {
        $quote = $PARAPARA->dblayer->get_quote_by_id( $id );
        if( $quote ) {
            quot_editform( array( 
                              'id'       => $quote->id,
                              'quote'    => $quote->quote,
                              'name'     => $quote->name,
                              'active'   => $quote->active,
                              ) );
        }
        else
            padm_warn("This Quote was not found.");
    }
    else
        padm_warn("Illegal id for quote has been given. Maybe someone has deleted it?");
}
elseif( $do == 'delete' ) {
    $id = $_GET['id'];
    if( preg_match('/^\d+$/', $id) ) {
        $deleted = $PARAPARA->dblayer->delete_quote( $id );
        if( $deleted )
            padm_info("Quote with id=$id is gone");
        else
            padm_warn("Quote not found and not deleted");
    } 
    else
        padm_warn("Illegal id for quote given");
}


echo "</div>\n";
return;

//--- only functions here ------------------------------------------------------------------

function quot_topmenu() {
    echo "<div class=\"padm_submenu\">",
        "<span class='item'><a href=\"admin.php?sec=quot&do=list\">List Quotes</a></span>",
        "<span class='item'><a href=\"admin.php?sec=quot&do=new\">New Quote</a></span>",
        "</div>";
}


function quot_editform( $param ) {
    if( $param == null )
        $param = array( 'active' => 1 );

    if( $param['id'] )
        echo "<h3>Edit Quote</h3>";
    else
        echo "<h3>Create Quote</h3>";

    $sf_quote      = htmlspecialchars( $param['quote'] );
    $sf_name       = htmlspecialchars( $param['name'] );

    $chk_active   = $param['active']   ? "checked='checked'" : "";
    
    $mark = '*';
    $nomark = '';
    $mark_quote = $param['err:quote']    ? $mark : $nomark;
    $mark_name  = $param['err:name']     ? $mark : $nomark;

    if( $param['status'] != 'ok') {
        $errorstrings = array(
            'err:quote' => "Error in quote. Quote empty or too long.",
            'err:name'  => "Error in quoter's name."
            );

        echo "<div class='error'>\n";
        foreach( $errorstrings as $key => $string ) {
            if( $param[ $key ] )
                echo "<div>", $string, "</div>\n";
        }
        echo "</div>\n";
    }

    echo '<form action="?sec=quot&do=form_action" method="POST">' . "\n";
    if( $param['id'] )
        echo "<input type='hidden' name='id' value='", htmlspecialchars( $param['id'] ), "' />\n";


    echo "<table id='padm_quote_edit_table'>\n";

    echo "<tr>\n";
    echo "<td class='mark'>$mark_quote</td>\n";
    echo "<td class='label'>Quote:</td>\n";
    echo "<td class='value'>",
        "<textarea  cols='50' rows='4' name='quote'>$sf_quote</textarea>",
        "</td>\n";
    echo "</tr>\n";


    echo "<tr>\n";
    echo "<td class='mark'>$mark_name</td>\n";
    echo "<td class='label'>Quoter's name:</td>\n";
    echo "<td class='value'>",
        "<input name='name' type='text' value='$sf_name' class='text'/>",
        "</td>\n";
    echo "</tr>\n";

    echo "<tr>\n";
    echo "<td class='mark'></td>\n";
    echo "<td class='label'>Quote is active:</td>";
    echo "<td class='value'>",
        "<input name='active' type='checkbox' value='1' $chk_active/>",
        "</td>";
    echo "</tr>\n";


    echo "<tr>\n";
    echo "<td class='buttons' colspan='3'>",
        "<button name='submit' class='padm_button' type='submit' value='1'>Submit</button>", 
        "<button name='cancel' class='padm_button' type='submit' value='1'>Cancel</button>",
        "</td>";
    echo "</tr>\n";

    echo "</table>\n";
    echo "</form>";
}


function quot_list( $pagenum, $pagesize ) {
    global $PARAPARA;
    $dbl = $PARAPARA->dblayer;

    $quotes = $dbl->get_quotes( $pagenum, $pagesize );

    $qclass = "active";

    if( !count($quotes ) ) {
        padm_info("No quotes here");
    }
    else {
        echo "<table id='padm_quote_list'>\n";
        echo "<thead>\n";
        echo "<tr>\n";
        echo 
            "<th>#id</th>",
            "<th>Quote</th>",
            "<th>Quoter</th>",
            "<th>active</th>",
            "<th>actions</th>\n";
        echo "</tr>\n";
        echo "</thead>\n";

        foreach( $quotes as $q ) {
            echo "<tr>";
            echo 
                "<td class='$qclass'>", htmlspecialchars( $q->id ), "</td>\n",
                "<td class='$qclass'>", htmlspecialchars( $q->quote ), "</td>\n",
                "<td class='$qclass'>", htmlspecialchars( $q->name ), "</td>\n",
                "<td class='$qclass'>", $q->active ? "yes" : "no", "</td>\n",
                "<td class='$bclass'>", 
                "<a href='?sec=quot&do=edit&id=$q->id'>edit</a>", "&nbsp;|&nbsp; ",
                "<a href='?sec=quot&do=delete&id=$q->id'>delete</a>",
                "</td>\n";
            echo "</tr>";
        }
        echo "</table>\n";
    }
}


function quot_validate_form() {
    $param = array();
    $errcount = 0;

    if($_POST['id'])
        $param['id'] = $_POST['id'];

    $param['quote']   = $_POST['quote'];
    $param['name']    = $_POST['name'];
    $param['active']  = $_POST['active'] ? 1: 0;

    if(!preg_match('/^.{1,200}$/ui', $param['quote'])) {
        $errcount++;
        $param['err:quote'] = 1;
    }

    if(!preg_match('/^.{1,59}$/ui', $param['name'])) {
        $errcount++;
        $param['err:name'] = 1;
    }

    if( $errcount == 0 )
        $param['status'] = 'ok';
    else
        $param['status'] = 'error';

    return $param;
}

function quot_update_db( $param ) {
    global $PARAPARA;
    $dbl = $PARAPARA->dblayer;

    $quote = new Quote(
        $param['id'],
        $param['quote'],
        $param['name'],
        null,
        $param['active']
        ); 

    //echo "<pre>", var_dump( $block ), "</pre>";

    $quote = $dbl->save_quote( $quote );
    return $quote->id;
}


?>